package com.iflytek.controller;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iflytek.domain.User;
import com.iflytek.service.UserService;
import com.iflytek.utils.AutoCreateIdUtils;
import com.iflytek.utils.MD5Utils;
import com.iflytek.utils.SmsSDKUtils;

@Controller
public class RegisterController {
	
	@Autowired
	private UserService us;
	
	// 获取表单中所有内容
	@RequestMapping(value = "/register/getForm.action")
	public String getForm(User user)throws IOException {
		/*User user = new User();
		try {
			BeanUtils.populate(user, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}*/
		String password=user.getPassword();
		String Md5Password=MD5Utils.md5(password);
		user.setUid(AutoCreateIdUtils.getUUID());
		user.setPassword(Md5Password);
		boolean isRegisterSuccess = us.findUserIsRegistSuccess(user);
		//String message = "";
		//String username = request.getParameter("username");
		if (isRegisterSuccess) {
			// if(request.getParameterMap().get("sex").clone().equals("男")) {
			// message+="您好"+username+"先生，恭喜您成功注册安信大酒店会员，欢迎您来本店预定。如有疑问可在线咨询"
			// + "我们定当为您提供优良的服务，谢谢";
			// }else
			// if(request.getParameterMap().get("sex").clone().equals("女")) {
			// message+="您好"+username+"女士，恭喜您成功注册安信大酒店会员，欢迎您来本店预定。如有疑问可在线咨询"
			// + "我们定当为您提供优良的服务，谢谢";
			// }
			// try {
			// MailUtils.sendMail(user.getUseremail(), message);
			// } catch (AddressException e) {
			// e.printStackTrace();
			// } catch (MessagingException e) {
			// e.printStackTrace();
			// }
			//response.sendRedirect(request.getContextPath() + "/login.jsp");
			return "redirect:login";
		} else {
			//response.sendRedirect(request.getContextPath() + "/register.jsp");
			return "redirect:register";
		}

	}
	
	// 发送验证码
	@RequestMapping(value = "/register/sendMessage.action")
	public void sendMessage(String userphone,HttpServletRequest request,HttpServletResponse response)throws IOException {
		String code = getCode(request);
		SmsSDKUtils.sendMessageCode(userphone, code);
		response.getWriter().write("true");
		response.flushBuffer();
	}

	// 获取验证码
	public String getCode(HttpServletRequest request) {
		Random rand = new Random();
		String code = "";
		for (int i = 0; i < 6; i++) {
			code += rand.nextInt(10);
		}
		request.getSession().setAttribute("code", code);
		return code;
		// String code1 = "123456";
		// request.getSession().setAttribute("code", code1);
		// return code1;
	}
	
	// 判断验证是否一样
	@RequestMapping(value = "/register/verification.action")
	public @ResponseBody
	String verification(String code,HttpSession session){
		String code1 = (String) session.getAttribute("code");
		boolean isExist = true;
		if (code != null && code1 != null) {
			if (session.getAttribute("code").equals(code)) {
				isExist = true;
			} else {
				isExist = false;
			}
		} else {
			isExist = false;
		}
		String json = "{\"isExist\":" + isExist + "}";
		//response.getWriter().write(json);
		return json;
	}
}
